package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingLevelDto;
import com.zzyl.entity.NursingLevel;
import com.zzyl.mapper.NursingLevelMapper;
import com.zzyl.service.NursingLevelService;
import com.zzyl.vo.NursingLevelVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class NursingLevelServiceImpl implements NursingLevelService {
    @Autowired
    private NursingLevelMapper nursingLevelMapper;

    /**
     * 获取所有护理等级数据
     * @return
     */
    @Override
    public List<NursingLevelVo> listAll() {
        // 获取所有护理等级数据
        List<NursingLevel> nursingLevels = nursingLevelMapper.listAll();
        // 将获取到的护理等级数据集合，将集合从NursingLevel对象转换成NursingLevelVo对象后返回
        return nursingLevels.stream().map(v -> BeanUtil.toBean(v, NursingLevelVo.class)).collect(Collectors.toList());
    }

    /**
     * 批量插入护理等级数据
     * @param nursingLevels 护理级别集合
     */
    @Override
    public void insertBatch(List<NursingLevelDto> nursingLevels) {
        List<NursingLevel> levels = BeanUtil.copyToList(nursingLevels, NursingLevel.class);
        nursingLevelMapper.insertBatch(levels);
    }

    // 根据id获取护理等级数据
    @Override
    public NursingLevelVo getById(Long id) {
        //根据id查询护理等级数据，封装到NursingLevel中
        NursingLevel nursingLevel = nursingLevelMapper.getById(id);
        //对象转换，NursingLevel -> NursingLevelVo
        NursingLevelVo nursingLevelVo = BeanUtil.toBean(nursingLevel, NursingLevelVo.class);
        return nursingLevelVo;
    }

    // 更新护理等级数据
    @Override
    public void update(NursingLevelDto nursingLevel) {
        //对象转换
        NursingLevel level = BeanUtil.toBean(nursingLevel, NursingLevel.class);
        nursingLevelMapper.update(level);
    }

    // 删除护理等级数据
    @Override
    public void delete(Long id) {
        nursingLevelMapper.delete(id);
    }

    /**
     * 分页查询护理等级列表数据
     * @param pageNum 页码
     * @param pageSize 页面大小
     * @param name 护理级别名称
     * @param status 护理级别状态
     * @return
     */
    @Override
    public PageResponse<NursingLevelVo> listByPage(Integer pageNum, Integer pageSize, String name, Integer status) {
        // 使用 pageHelper插件 进行分页查询护理等级数据
        PageHelper.startPage(pageNum, pageSize);
        // 根据 pageNum  pageSize   name   status 进行分页查询数据操作，封装到 Page<List<NursingLevel>>  中
        Page<List<NursingLevel>> page = nursingLevelMapper.listByPage(pageNum, pageSize, name, status);
        // 通过封装到Page<List<NursingLevel>>中的数据，在NursingLevelVo类加载阶段封装数据
        // 封装分页信息
        return  PageResponse.of(page, NursingLevelVo.class);
    }

    @Override
    public void enableOrDisable(Long id, Integer status) {
        nursingLevelMapper.updateStatus(id, status);
    }

    /**
     * 根据id
     * @param ids
     * @return
     */
    @Override
    public List<NursingLevel> listAllByPlanIds(List<Long> ids) {
        return nursingLevelMapper.listAllByPlanIds(ids);
    }

    /**
     * 插入护理等级数据护理等级信息
     * @param nursingLevel 护理级别
     */
    @Override
    public void insert(NursingLevelDto nursingLevel) {
        //对象转换
        NursingLevel level = BeanUtil.toBean(nursingLevel, NursingLevel.class);
        nursingLevelMapper.insert(level);
    }
}

